//
//  shopTopicView.m
//  ILiangCang
//
//  Created by ZX on 16-4-16.
//  Copyright (c) 2016年 CRQ. All rights reserved.
//

#import "shopTopicView.h"
#import "topicCell.h"
#import "httpEngine.h"
#import "dataModel.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@implementation shopTopicView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
        
        [self addTableView];
     }

    return self;
}

-(void)addTableView
{
    
    tabView = [[UITableView alloc]initWithFrame:self.frame];
    tabView.delegate =self;
    tabView.dataSource = self;
    [self addSubview:tabView];
    [tabView registerClass:[topicCell class] forCellReuseIdentifier:@"topicCell"];
    tabView.rowHeight = WIDTH/2;
    
    [tabView addHeaderWithTarget:self action:@selector(headerRefreshing)];
    
    [httpEngine requestToGetShopTopicCompletion:^(NSMutableArray *topicArray, NSError *error) {
        _dataArray = topicArray;
        [tabView reloadData];
    }];


    
}
-(void)headerRefreshing
{
    [httpEngine requestToGetShopTopicCompletion:^(NSMutableArray *topicArray, NSError *error) {
        _dataArray = topicArray;
        [tabView reloadData];
    }];
    
    [tabView headerEndRefreshing];

    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * indentifer = [NSString stringWithFormat:@"topicCell"];
    topicCell * cell = [tableView dequeueReusableCellWithIdentifier:indentifer forIndexPath:indexPath];
    dataModel * data = _dataArray[indexPath.row];
    if (!cell)
    {
        cell = [[topicCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"topicCell"];
        
    }
    [cell.coverImage sd_setImageWithURL:data.coverImage];
    cell.titleLabel.text = data.topicName;
    
    return cell;
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(topicCellDidSelectedRow:with:)])
    {
        [self.delegate topicCellDidSelectedRow:indexPath.row with:_dataArray];
    }
}

@end
